from datetime import datetime
from tortoise import fields, models
from app.settings.config import settings
import asyncio


class BaseModel(models.Model):

    id = fields.BigIntField(pk=True, index=True)

    async def to_dict(self,
                      m2m: bool = False,
                      exclude_fields: list[str] | None = None):
        if exclude_fields is None:
            exclude_fields = []
        d = {}
        for filed in self._meta.db_fields:
            value = getattr(self, filed)
            if isinstance(value, datetime):
                value = value.strftime(settings.DATETIME_FORMAT)
            d[filed] = value
        if m2m:
            tasks = [
                self.__fetch_m2m_fields(field)
                for field in self._meta.m2m_fields
                if field not in exclude_fields
            ]
            results = await asyncio.gather(*tasks)
            for field, values in results:
                d[field] = values
        return d

    async def __fetch_m2m_fields(self, field):
        values = [value for value in await getattr(self, field).all().values()]
        for value in values:
            value.update((k, v.strftime(settings.DATETIME_FORMAT))
                         for k, v in value.items() if isinstance(v, datetime))
        return field, values

    class Meta:
        abstract = True


class UUIDModel(BaseModel):
    uuid = fields.UUIDField(unique=True, pk=False, index=True)


class TimestampMixin:
    created_at = fields.DatetimeField(auto_now_add=True)
    updated_at = fields.DatetimeField(auto_now=True)
